﻿using System;
using System.Text;

namespace Hjt212Parser
{
    public static class Crc
    {
        private static CrcType GetCrcType(string value)
        {
            if (string.IsNullOrEmpty(value)) throw new Exception("参数不能为空！");
            value = value.ToLower();
            if (value.Contains("total"))
            {
                return CrcType.GuHaoTotal;
            }
            if (value.Contains("both"))
            {
                return CrcType.Both;
            }
            return CrcType.Monitor;
        }
        private static string GenCrcGuHaoTotal(string databuffa)
        {
            var databuff = Encoding.Default.GetBytes(databuffa);
            int nLength = databuff.Length;
            int crc = 0xFFFF;
            for (int i = 0; i < nLength; i++)
            {
                crc ^= databuff[i];
                for (int j = 0; j < 8; j++)
                {
                    var tmp = crc & 0x0001;
                    crc = crc >> 1;
                    if (tmp != 0)
                        crc = (crc ^ 0xA001);
                }
            }
            return crc.ToString("X4");
        }
        private static string GenCrcMonitor(string databuffa)
        {
            byte[] databuff = Encoding.Default.GetBytes(databuffa);
            int wkg = 0xFFFF;
            for (int j = 0; j < databuff.Length; j++)
            {
                wkg = (wkg >> 8) & 0x00FF;
                wkg ^= databuff[j];
                for (byte i = 0; i < 8; i++)
                {
                    if ((wkg & 0x0001) != 0)
                    {
                        wkg = (wkg >> 1) ^ 0xa001;
                    }
                    else
                    {
                        wkg = wkg >> 1;
                    }
                }
            }
            return wkg.ToString("X4");
        }
        public static string GenCrc(string databuffa, CrcType crcType)
        {
            switch (crcType)
            {
                case CrcType.Monitor:
                    return GenCrcMonitor(databuffa);
                case CrcType.GuHaoTotal:
                    return GenCrcGuHaoTotal(databuffa);
                case CrcType.Both:
                    return GenCrcMonitor(databuffa) + "%" + GenCrcGuHaoTotal(databuffa);
            }
            return "";
        }
        public static string WrapByCrc(string result, string crcTypeStr)
        {
            var crcType = GetCrcType(crcTypeStr);
            switch (crcType)
            {
                case CrcType.Monitor:
                    return "##" + result.Length.ToString("D4") + result + GenCrc(result, crcType) + "\r\n";
                case CrcType.GuHaoTotal:
                    return "##" + result.Length.ToString("D4") + result + GenCrc(result, crcType) + "\r\n";
                case CrcType.Both:
                    return "##" + result.Length.ToString("D4") + result + GenCrc(result, CrcType.Monitor) + "\r\n" + "%&%" + "##" + result.Length.ToString("D4") + result + GenCrc(result, CrcType.GuHaoTotal) + "\r\n";
            }
            return "";
        }
    }

    public enum CrcType
    {
        Monitor,
        GuHaoTotal,
        Both
    }


}
